package com.wondertek.mist;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageButton;
import android.widget.Toast;

import com.bjca.xinshoushu.SignatureAPI;
import com.bjca.xinshoushu.Interface.OnSignatureResultListener;
import com.bjca.xinshoushu.beans.DataObj;
import com.wondertek.mist.plugins.SignaturePlugin;
import com.wondertek.mist.utils.RUtil;

public class PolicySignActivity extends Activity implements OnClickListener{


	public static final String SIGNATURE_SIGN = "Sign";
	public static final String BITMAP_TWENTY = "bitmap20";
	public static final String BITMAP_THIRTY = "bitmap30";
	private SignatureAPI api;
	private String pdfHash;
	private String name;
	private String card;
	private String callback;
	private String bsData;
	private Bitmap bitmap20;
	private Bitmap bitmap30;
	private String params;
	private int index;
	private ImageButton thirrtyButton;
	private ImageButton twentyButton;
	private ImageButton submitButton;
	private ImageButton backButton;
	


	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(RUtil.layout(this,"activity_signaturer"));

		Bundle bundle = getIntent().getExtras();

		name = bundle.getString(SignaturePlugin.MIST_NAME_EXTRA);
		card = bundle.getString(SignaturePlugin.MIST_CARD_EXTRA);
		callback = bundle.getString(SignaturePlugin.MIST_CALLBACK_EXTRA);
		bsData = bundle.getString(SignaturePlugin.MIST_BSDATA_EXTRA);

		thirrtyButton = (ImageButton) findViewById(RUtil.id(this,"ThirrtyButton"));
		twentyButton = (ImageButton) findViewById(RUtil.id(this,"TwentyButton"));
		submitButton = (ImageButton) findViewById(RUtil.id(this,"submit"));
		backButton = (ImageButton) findViewById(RUtil.id(this,"Back"));

		twentyButton.setOnClickListener(this);
		thirrtyButton.setOnClickListener(this);
		submitButton.setOnClickListener(this);
		backButton.setOnClickListener(this);

		api = new SignatureAPI(this, null);
		api.addDataObj(new DataObj(13));
		api.addDataObj(new DataObj(14));
		api.addDataObj(new DataObj(16));
		api.addSignatureObj(30, "经保险人明确说明，本人已完全理解了责任免除、免赔规定等免除保险人责任的条款");
		api.addSignatureObj(20, "请" + name + "签署协议本人已阅读保险条款", 1, name.length());
		api.setTID("3001");
		api.setData(16, "100007");//神行车保渠道号
		api.commit();
		String serial_string = "{\"bjcaxssrequest\":{\"submitinfo\":[{\"username\":\""
				+ name + "\",\"identitycardnbr\":\"" + card + "\"}]}}";

		api.setData(13, bsData);
		api.setData(14, serial_string);
		final Context context=this;
		api.setOnSignatureResultListener(new OnSignatureResultListener() {

			@Override
			public void onDialogDismiss(int arg1) {

				// Toast.makeText(SignatureActivity.this,
				// "onDialogDismiss"+arg1, Toast.LENGTH_SHORT).show();
			}

			@Override
			public void onDialogCancel(int arg2) {
				/*
				 * if(bitmap30!=null && arg2 ==30){//投保人已经声明签字 Bitmap bitmap =
				 * getBitmapById
				 * (SignatureActivity.this,R.drawable.tbr_btn_click);
				 * thirrtyButton.setImageBitmap(bitmap); }else if(bitmap20!=null
				 * && arg2 ==20){ Bitmap bit =
				 * getBitmapById(SignatureActivity.this
				 * ,R.drawable.qm_btn_click); twentyButton.setImageBitmap(bit);
				 * } Toast.makeText(SignatureActivity.this,
				 * "onDialogCancel"+arg2, Toast.LENGTH_SHORT).show();
				 */
			}

			@Override
			public void onDataDeleted(boolean arg3) {
				// Toast.makeText(SignatureActivity.this, arg3,
				// Toast.LENGTH_SHORT).show();
			}

			@Override
			public void onBufferSaved(boolean arg0) {
				// Toast.makeText(SignatureActivity.this, arg0,
				// Toast.LENGTH_SHORT).show();
			}

			@Override
			public void onSignatureResult(int arg0, Bitmap arg1) {

				if (arg0 == 20) {
					bitmap20 = arg1;

					if(bitmap20!=null){ 
//						Bitmap bit =
//							getBitmapById(SignatureActivity.this,R.drawable.qm_btn_click);
					twentyButton.setBackgroundResource(RUtil.drawable(context,"qm_btn_click")); }

				}

				if (arg0 == 30) {
					bitmap30 = arg1;

					if(bitmap30!=null){//投保人已经声明签字 
//						Bitmap bitmap =getBitmapById
//								(SignatureActivity.this,R.drawable.tbr_btn_click);
						thirrtyButton.setBackgroundResource(RUtil.drawable(context,"tbr_btn_click")); }

				}

			}
		});

	}
	

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(RUtil.menu(this,"signature"), menu);
		return true;
	}


	@SuppressLint("NewApi")
	@Override
	public void onClick(View v) {
		if(v.getId()==RUtil.id(this, "TwentyButton")){
			api.showSingleInputDialog(20);
		}else if(v.getId()==RUtil.id(this, "ThirrtyButton")){
			api.showMassInputDialog(30);
		}else if(v.getId()==RUtil.id(this, "submit")){
			if (bitmap30 != null && bitmap20 != null) {

				params = api.getUploadDataGram();
				// Toast.makeText(SignatureActivity.this, params,
				// Toast.LENGTH_SHORT).show();
				File f01 = new File(Environment.getExternalStorageDirectory(),
						"cvd01.png");
				File f02 = new File(Environment.getExternalStorageDirectory(),
						"cvd02.png");

				try {
					if (!f01.exists()) {
						f01.createNewFile();
					}

					if (!f02.exists()) {
						f02.createNewFile();
					}
				} catch (IOException e) {
					// TODO Auto-generated catch block

					e.printStackTrace();
				}
				FileOutputStream fOut01 = null;
				FileOutputStream fOut02 = null;
				try {
					fOut01 = new FileOutputStream(f01);
					fOut02 = new FileOutputStream(f02);
				} catch (FileNotFoundException e) {
					// TODO Auto-generated catch block

					e.printStackTrace();
				}
				bitmap20.compress(Bitmap.CompressFormat.PNG, 100, fOut01);
				bitmap30.compress(Bitmap.CompressFormat.PNG, 100, fOut02);

				String file_path20 = Environment.getExternalStorageDirectory()
						.toString() + "/cvd01.png";
				String file_path30 = Environment.getExternalStorageDirectory()
						.toString() + "/cvd02.png";

				try {
					fOut01.flush();
					fOut02.flush();
				} catch (IOException e) {
					// TODO Auto-generated catch block

					e.printStackTrace();
				}
				try {
					fOut01.close();
					fOut02.close();
				} catch (IOException e) {

					e.printStackTrace();
				}

				Intent intent = new Intent(PolicySignActivity.this,
						SignaturePlugin.class);
				intent.putExtra(SIGNATURE_SIGN, params);
				intent.putExtra(BITMAP_TWENTY, file_path20);
				intent.putExtra(BITMAP_THIRTY, file_path30);

				setResult(RESULT_OK, intent);
				//重置API
				api.reset();
				finish();

			} else if (bitmap20 == null) {// 未签名
				Toast.makeText(PolicySignActivity.this, "请签署姓名",
						Toast.LENGTH_SHORT).show();
			} else if (bitmap30 == null) {// 投保人未声明签字
				Toast.makeText(PolicySignActivity.this, "请签署投保人申明",
						Toast.LENGTH_SHORT).show();
			}
		}else if(v.getId()==RUtil.id(this, "Back")){
			this.finish();
		}
		
	}


	


}
